﻿//配置插件目录
layui.config({
    base: '/mods/'
});
layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'tree','laypage'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var $ = layui.jquery;
    var tree = layui.tree;
    var laypage = layui.laypage;
    var pageSize = 10;
    var i = 0;
    $(function () {
        ShowInfo(1);
    });
    // 单删除

    //第一个参数为操作内容，例如：登录了系统，修改/删除/添加/查询了日志表（或其他表，每次调用根据进行的操作写）
    //第二个参数为修改专用，将修改前的参数转为json字符串格式传入
    //第三个参数是将添加或删除的数据转为json传入，若是修改方法则把修改后的数据转为json字符串传入
    function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
        const data = {
            olContext: OperationalContext,
            PreOperation: PreOperation,
            AfterOperation: AfterOperation
        };
        // alert(JSON.stringify(data));
        $.ajax({
            url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
            type: 'post',
            dataType: 'json',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            contentType: 'application/json', // 添加正确的 Content-Type 头部
            data: JSON.stringify(data),
            success: function (res) {
                // alert(res)
                //getListData(1);
                console.log(res)
            }
        })
    }
    function deleteData(id) {
        $.ajax({
            url: `http://8.140.245.229:8000/webroute/api/Setting/HandleDelForm?Id=${id}`,
            type: 'delete',
            success: function (res) {
                if (res.data != 0) {
                    layer.msg('删除成功');
                    ShowInfo(1);
                } else {
                    layer.msg('删除失败');
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }
    // 监听工具条，实现删除功能
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.FormId); // 传递正确的数据项ID
                layer.close(index);
            });
        } else if (obj.event == 'edit') {
            location.href = `/Event/EventEdit?id=${data.EventId}`;
        }

    });
    //渲染
    laydate.render({
        elem: '#storageDate'
    });
    function ShowInfo(pageIndex) {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/Form/HandleFormList",
            type: 'get',
            dataType: 'json',
            data: {
                FormName: $("#FormName").val(),
                pageIndex: pageIndex,
                pageSize: pageSize,
            },
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                AddLogInfo("查询表单管理");
                table.render({
                    elem: '#test',
                    cols: [[
                        {
                            type: 'checkbox', align: 'center',
                        },
                        {
                            title: '序号', align: 'center',
                            templet: function (data) { return ++i + ((pageIndex - 1) * pageSize) }
                        },
                        { field: 'FormName', title: '表单名称', align: 'center' },
                        { field: 'FromDesc', title: '表单描述', align: 'center' },
                        { title: '操作', width: 150, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true }
                    ]],
                    //style: "width:500px;height:500px;",
                    width: 1300,
                    height: 600,
                    data: res.Datas,
                });
                laypage.render({
                    //挂载的分页id
                    elem: 'demo-laypage-all',
                    count: res.TotalCount,
                    limit: pageSize,
                    curr: pageIndex,
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                    jump: function (obj, first) {
                        console.log(obj);
                    }
                })
            }
        })
    }
    $("#searchBtn").on('click', function () {
        ShowInfo(1);
    })
})
